package com.jp.knowledge.my.fragment;

import android.os.Bundle;
import android.view.View;

import com.google.gson.JsonObject;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.JpApplication;
import com.jp.knowledge.fragment.TeamCircleDataShowFragment;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.TeamCircleModel;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.my.adapter.FeedbackAdapter;

import java.util.ArrayList;

/**
 * Created by 528787067@qq.com on 2017/5/8.
 */

public class FeedbackFragment extends TeamCircleDataShowFragment {

    public static FeedbackFragment newInstance(UserData userData){
        FeedbackFragment fragment = new FeedbackFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("userData", userData);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initCachePath() {
        cachePath = getContext().getCacheDir().getPath() + "/feedback" + HttpHelper.uuid + ".dat";
    }

    @Override
    protected void initAdapter() {
        teamCircleAdapter = new FeedbackAdapter(context, teamCircleModels);
    }

    @Override
    protected void commitComment(String content, int position) {
        TeamCircleModel.CommentsBean comment = new TeamCircleModel.CommentsBean();
        comment.setCommentType(3);
        comment.setContent(content);
        try {
            UserData userData = ((JpApplication)getActivity().getApplication()).getUserData();
            comment.setNickname(userData.getNickname());
            comment.setUid(userData.getUuid());
        } catch (Exception e){}
        TeamCircleModel.CommentsBean.ReplyBean replyBean = new TeamCircleModel.CommentsBean.ReplyBean();
        replyBean.setUid(toUid);
//        replyBean.setNickname(toNickName);
        comment.setReply(replyBean);

        JsonObject param = new JsonObject();
        param.addProperty("content", comment.getContent());
        param.addProperty("id", teamCircleModels.get(position).getId());
        param.addProperty("toUid", comment.getReply().getUid());
        HttpHelper.getInstance(context)
                .feedbackComment(param, position*10+COMMENT, this);

        if(teamCircleModels.get(position).getComments() == null)
            teamCircleModels.get(position).setComments(new ArrayList<TeamCircleModel.CommentsBean>());
        teamCircleModels.get(position).getComments().add(0, comment);
        teamCircleAdapter.notifyItemChanged(position);
    }

    @Override
    protected void doDelete(int position) {
        JsonObject param = new JsonObject();
        param.addProperty("id", teamCircleModels.get(position).getId());
        HttpHelper.getInstance(context).feedbackDelete(param, position*10+DELETE, this);

        deleteDialog.cancel();
        teamCircleModels.remove(position);
        teamCircleAdapter.notifyDataSetChanged();
        if (teamCircleModels.size() == 0)
            contentView.findViewById(R.id.team_circle_empty_bg).setVisibility(View.VISIBLE);
        else
            contentView.findViewById(R.id.team_circle_empty_bg).setVisibility(View.GONE);
    }

    @Override
    protected void getData(int type, int mode) {
        long time = System.currentTimeMillis();
        if(teamCircleModels != null && teamCircleModels.size() > 0 && mode == MODE_1) {
            time = teamCircleModels.get(teamCircleModels.size() - 1).getTime();
        }
        JsonObject param = new JsonObject();
        param.addProperty("time", time);
        param.addProperty("mode", mode);
        HttpHelper.getInstance(context).feedbackList(param, 10 + mode, this);
    }

    @Override
    protected void updateRemindView() {
        teamCircleHeader.setVisibility(View.GONE);
    }
}
